﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestProject.ServiceReference1;
using Microsoft.Win32;
using System.Windows;
using System.IO;

namespace TestProject
{
    [TestClass]
    public class UnitTest
    {
        [TestMethod]
        public void TestSaveVideoInServerEnvironment()
        {
            VideoClass video = new VideoClass();
            using (var proxy = new ServiceReference1.Service1Client())
            {
                proxy.SaveVideoToServiceEnvironment(video);
            }

            //OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "Video Files (*.wmv)|*.wmv";
            //ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
            //bool? res = ofd.ShowDialog();
            //if (res.HasValue)
            //    if (res.Value)
            //    {
            //        //Uri uri = new Uri(ofd.FileName);
            //        FileStream fileStream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
            //        BinaryReader binaryReader = new BinaryReader(fileStream);
            //        int numOfBytes = ((int)(new FileInfo(ofd.FileName).Length));
            //        byte[] videoBuffer = binaryReader.ReadBytes(numOfBytes);

            //        ofd.Filter = "Image Files (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
            //        res = ofd.ShowDialog();
            //        if (res.HasValue)
            //            if (res.Value)
            //            {
            //                fileStream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
            //                binaryReader = new BinaryReader(fileStream);
            //                numOfBytes = ((int)(new FileInfo(ofd.FileName).Length));
            //                byte[] samllImageBuffer = binaryReader.ReadBytes(numOfBytes);

            //                VideoClass video = new VideoClass();
            //                string tmp = ofd.FileName.ToString().Split('.')[0];
            //                string[] arr = tmp.Split('\\');
            //                video.name = arr[arr.Length - 1];
            //                video.videoByte = videoBuffer;
            //                video.samllImageBuffer = samllImageBuffer;

            //                using (var proxy = new ServiceReference1.Service1Client())
            //                {
            //                    proxy.SaveVideoToServiceEnvironment(video);
            //                }
            //            }
            //    }
        }

        [TestMethod]
        public void TestGetVideoById()
        {
            using (var proxy = new ServiceReference1.Service1Client())
            {
                var tmp = proxy.GetVideoById(4);
                Assert.IsInstanceOfType(tmp, typeof(VideoClass));
            }
        }

        [TestMethod]
        public void TestGetVideoByIdFail() 
        {
            using (var proxy = new ServiceReference1.Service1Client())
            {
                var tmp = proxy.GetVideoById(5000);
                Assert.IsNull(tmp);
            }
        }

        [TestMethod]
        public void TestAddVideoToDB()
        {
            //OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "Video Files (*.wmv)|*.wmv";
            //ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
            //bool? res = ofd.ShowDialog();
            //if (res.HasValue)
            //    if (res.Value)
            //    {
            //        //Uri uri = new Uri(ofd.FileName);
            //        FileStream fileStream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
            //        BinaryReader binaryReader = new BinaryReader(fileStream);
            //        int numOfBytes = ((int)(new FileInfo(ofd.FileName).Length));
            //        byte[] videoBuffer = binaryReader.ReadBytes(numOfBytes);

            //        ofd.Filter = "Image Files (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
            //        res = ofd.ShowDialog();
            //        if (res.HasValue)
            //            if (res.Value)
            //            {
            //                fileStream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
            //                binaryReader = new BinaryReader(fileStream);
            //                numOfBytes = ((int)(new FileInfo(ofd.FileName).Length));
            //                byte[] samllImageBuffer = binaryReader.ReadBytes(numOfBytes);

            //                VideoClass video = new VideoClass();
            //                string tmp = ofd.FileName.ToString().Split('.')[0];
            //                string[] arr = tmp.Split('\\');
            //                video.name = arr[arr.Length - 1];
            //                video.videoByte = videoBuffer;
            //                video.smallImageByte = samllImageBuffer;

            //                using (var proxy = new ServiceReference1.Service1Client())
            //                {
            //                    bool resault = proxy.SaveVideoToDB(video);
            //                    Assert.IsTrue(resault);
            //                }
            //            }
            //    }
        }

        [TestMethod]
        public void TestDeleteVideoById()
        {
            using (var proxy = new ServiceReference1.Service1Client())
            {
                bool res = proxy.DeleteVideoById(7);
                Assert.IsTrue(res);
            }
        }

        [TestMethod]
        public void TestUpdateVideoById()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Video Files (*.wmv)|*.wmv";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
            bool? res = ofd.ShowDialog();
            if (res.HasValue)
                if (res.Value)
                {
                    //Uri uri = new Uri(ofd.FileName);
                    FileStream fileStream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                    BinaryReader binaryReader = new BinaryReader(fileStream);
                    int numOfBytes = ((int)(new FileInfo(ofd.FileName).Length));
                    byte[] videoBuffer = binaryReader.ReadBytes(numOfBytes);

                    ofd.Filter = "Image Files (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
                    res = ofd.ShowDialog();
                    if (res.HasValue)
                        if (res.Value)
                        {
                            fileStream = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                            binaryReader = new BinaryReader(fileStream);
                            numOfBytes = ((int)(new FileInfo(ofd.FileName).Length));
                            byte[] samllImageBuffer = binaryReader.ReadBytes(numOfBytes);

                            VideoClass video = new VideoClass();
                            string tmp = ofd.FileName.ToString().Split('.')[0];
                            string[] arr = tmp.Split('\\');
                            string name = arr[arr.Length - 1];

                            using (var proxy = new ServiceReference1.Service1Client())
                            {
                                bool result = proxy.UpdateVideoById(2, name, videoBuffer, samllImageBuffer);
                                Assert.IsTrue(result);
                            }
                        }
                }
        }

        [TestMethod]
        public void TestGetAllThumbnail()
        {
            using (var proxy = new ServiceReference1.Service1Client())
            {
                var thumbnails = proxy.GetAllThumbnailVideoAvailable();
                Assert.IsInstanceOfType(thumbnails, typeof(ListViewClass));
            }
        }

        [TestMethod]
        public void TestGetAllBannersFromVideo()
        {
            using (var proxy = new ServiceReference1.Service1Client())
            {
                var banners = proxy.GetAllBannersForVudeo(4);
                Assert.IsInstanceOfType(banners, typeof(BannerClass[]));
            }
        }

    }
}
